

import Module from "./Module";
import XlsxDataManager from "../../cfw/xlsx/XlsxDataManager";
import { ResType } from "../res/ResInterface";
import ResItem from "../res/ResItem";

//模块id
export enum ModuleID {
    PUBLIC,
    LOGIN,
    LOADING,
    LOBBY,
    OUT_DOOR,//室外
    DECORATION,
    BORN,
    SMALL_GAME,
    COOKIE,
    CRAZY_CLICK,
    GIFT,
    MAX
}

//有分包的模块就有bundle的名字，没有就是使用cc.resource 

let ModuleConfig = [
    //public
    { name: 'public', isBundle: false, bundleName: '' },
    //login
    { name: 'login', isBundle: false, bundleName: '' },
    //loading
    { name: 'loading', isBundle: false, bundleName: '' },
    //lobby
    { name: 'lobby', isBundle: true, bundleName: 'lobby' },
    //outdoor
    { name: 'outdoor', isBundle: true, bundleName: 'outdoor' },
    { name: 'decoration', isBundle: true, bundleName: 'decoration' },
    //born
    { name: 'born', isBundle: false, bundleName: '' },
    //smallgame
    { name: 'smallgame', isBundle: true, bundleName: 'lobby' },
    //cookie 
    { name: 'cookie', isBundle: true, bundleName: 'cookery' },
    { name: 'crazyClick', isBundle: true, bundleName: 'crazyClick' },
    { name: 'gift', isBundle: true, bundleName: 'gift' },
]
export default class ModuleManager {


    private static mgrMap: Module[] = []

    private static moduleID: ModuleID = ModuleID.LOADING;


    static dataManager: XlsxDataManager;

    private static bundleList: number[] = []

    static getBundleList() {
        return this.bundleList
    }

    static init(projectName: string, hasSubPackage: boolean = true) {
        for (let index = 0; index < ModuleConfig.length; index++) {
            let config = ModuleConfig[index]
            config.isBundle = config.isBundle ? hasSubPackage : config.isBundle;
            this.mgrMap[index] = new Module(projectName, config, hasSubPackage);
            if (config.isBundle) {
                this.bundleList.push(index)
            }
        }
        this.mgrMap[ModuleID.PUBLIC].setAudio()
        this.dataManager = new XlsxDataManager()
    }

    static getAudio(id: ModuleID = this.moduleID) {
        return this.mgrMap[id].getAudio()
    }

    static publicAudio() {
        return this.mgrMap[ModuleID.PUBLIC].getAudio()
    }

    static publicLoader() {
        return this.mgrMap[ModuleID.PUBLIC].getLoader()
    }


    static loadRes(url: string, type: ResType, callback: (err: string, res: ResItem) => void) {
        this.getLoader().loadRes(url, type, callback)
    }

    static getName(id: ModuleID = this.moduleID) {
        return this.mgrMap[id].getName()
    }

    static setLoaderByBundle(id: ModuleID = this.moduleID, bundle: cc.AssetManager.Bundle) {
        this.mgrMap[id].setLoaderByBundle(bundle)
    }

    static loadBundle(moduleID: ModuleID, callback: Function) {
        this.publicLoader().loadRes(this.getName(moduleID), ResType.AssetBundle, (err, res: ResItem) => {
            if (err) {
                return;
            }
            this.setLoaderByBundle(moduleID, res.getRes())
            callback();
        })
    }

    static setModuleID(id: ModuleID) {
        this.moduleID = id;
    }

    static getLoader(id: ModuleID = this.moduleID) {
        return this.mgrMap[id].getLoader()
    }

    static hasLoader(id: ModuleID = this.moduleID) {
        return this.mgrMap[id].hasLoader()
    }

}